<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>game Demo</title>
</head>
<body>
<div id="container">
    <ul>
        <li v-for="msg in messages">[<span style="color:red;">{{msg.name}}</span>]{{msg.content}}</li>
    </ul>
    <div class="controls">
        <input type="text" v-model="nickname">
        <input type="text" v-model="inputMessage">
        <input type="button" v-on:click="updatepwdMessage" value="updatepwd">
        <input type="button" v-on:click="sendMessage" value="Send">
        <input type="button" v-on:click="loginMessage" value="login">
        <input type="button" v-on:click="joinMessage" value="join">
    </div>
</div>
<script src="http://cdnjs.cloudflare.com/ajax/libs/vue/1.0.26/vue.min.js" type="text/javascript"></script>
<!--[starx websocket library](https://github.com/topfreegames/pitaya-client-websocket)-->
<script src="protocol.js" type="text/javascript"></script>
<script src="starx-wsclient.js" type="text/javascript"></script>
<script>
    var v = new Vue({
        el: "#container",
        data: {
            nickname:'guest' + Date.now(),
            inputMessage:'',
            messages: []
        },
        methods: {
            sendMessage: function () {
                console.log("sendMessage");
                starx.notify('room.message', {name: this.nickname, content: this.inputMessage});
            },
            updatepwdMessage: function () {
                console.log("updatepwdMessage");
                starx.request('login.updatepwd', {account_name: this.nickname, pwd_md5: this.inputMessage}, onLogin);
            },
            loginMessage: function () {
                console.log("loginMessage");
                starx.request('login.login', {account_name: this.nickname, pwd_md5: this.inputMessage}, onLogin);
            },
           joinMessage: function () {
                console.log("joinMessage");
                starx.request("room.join", {}, join);
            }
        }
    });

    var onMessage = function (msg) {
        v.messages.push(msg)
    };

    var join = function (data) {
        console.log(data);
        if(data.code === 0) {
            v.messages.push({name:'system', content:data.result});
            starx.on('onMessage', onMessage)
        }
    };

    var onNewUser = function (data) {
        console.log(data);
        v.messages.push({name:'system', content:data.content});
    };

    var onMembers = function (data) {
        console.log(data);
        v.messages.push({name:'system', content: "members: "+data.members});
    };
    var onLogin = function (data) {
        console.log(data);
        v.messages.push({name:'system', content: "login res: "+data.code+data.result});
    };


    starx.init({host: '127.0.0.1', port: 3850, path: '/'}, function () {
        console.log("initialized");
        starx.on("room.onNewUser", onNewUser);
        starx.on("room.onMembers", onMembers);
        starx.on("login.login", onLogin);
    })
</script>
</body>
</html>
